<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/8/4
 * Time: 11:26
 */

namespace Admin\Controller;


class AdcontentController extends AdminController
{
    public function _myinitialize()
    {
        $this->db = M('ad_content');
        $this->db1 = M('ad');
        $this->db2 = M('ad_code_template');
        $this->username = session('user_auth.username');
    }

    //广告详情列表
    public function lst(){
        $where['status'] = 1;
        $ad_id = I('ad_id');
        $ad_name = I('ad_name');
        if (!empty($ad_id)) {
            $where['ad_id'] = $ad_id;
            $lists = $this->db->where($where)->select();
            $this->assign('ad_id', $ad_id);
            $this->assign('ad_name', $ad_name);
            $this->assign('lists', $lists);
        }
        $this->display();
    }

    //广告详情新增
    public function add(){
        $arr = array();
        if (IS_POST) {
            //需要在隐藏匿中传过来
            $arr = array(
                "ad_id" => I('ad_id'),
                "ad_name" => I('ad_name'),
            );
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['update_user'] = $this->username;
                $data['update_time'] = time();
            }
            $rid = $this->db->add($data);
            if ($rid === false) {
                 $this->error($this->db->getDbError());
            }
            $this->redirect('lst', $arr);
        }else{
            $where['status'] = 1;
            $ad_id = I('ad_id');
            $ad_name = I('ad_name');
            if (!empty($ad_id)) {
                $where['ad_id'] = $ad_id;
                $lists = $this->db->where($where)->select();
                $this->assign('ad_id', $ad_id);
                $this->assign('ad_name', $ad_name);
            }
            $this->assign('istype',"add");
            $this->display();
        }
    }

    //广告详情编辑
    public function edit(){
        $arr = array();
        if (IS_POST) {
            //需要在隐藏匿中传过来
            $arr = array(
                "ad_id" => I('ad_id'),
                "ad_name" => I('ad_name'),
            );
            $data = $this->db->create();
            if ($data === false) {
                $this->error($this->db->getError());
            } else {
                $data['update_user'] = $this->username;
                $data['update_time'] = time();
            }
            $rid = $this->db->save($data);
            
             if ($rid === false) {
                 $this->error($this->db->getDbError());
             }
             $this->redirect('lst',$arr);
        } else { //回显
            $id = I('id');
            $data = $this->db->where(array('id' => $id))->find();
            
            $where['status'] = 1;
            $ad_id = I('ad_id');
            $ad_name = I('ad_name');
            if (!empty($ad_id)) {
                $where['ad_id'] = $ad_id;
                $lists = $this->db->where($where)->select();
                $this->assign('ad_id', $ad_id);
                $this->assign('ad_name', $ad_name);
            }
            $this->assign('data',$data);
            $this->assign('istype',"edit");
            $this->display('add');
        }
    }

    //广告详情删除
    public function del()
    {
        $id = I('id');
        $arr = array(
            "ad_id" => I('ad_id'),
            "ad_name" => I('ad_name'),
        );
        if (!empty($id)) {
            $data = array(
                'status' => 0,
                'update_time' => time(),
                'update_user' => $this->username,
            );
            $this->db->where(array('id' => $id))->save($data);
            $this->redirect('lst',$arr);
        }
    }

    //生成广告代码
    public function create_ad()
    {
        $ad_id = I('ad_id');
        $info=array();
        //当前广告id的模板
        if (!empty($ad_id)) {
            //根据广告id在广告表中查出模板id
            $template_id = $this->db1->where(array('status' => 1, 'id' => $ad_id))->getField('template_id');
            //根据模板id去模板表中找到相应的模板
            $template_content = $this->db2->where(array('status' => 1, 'id' => $template_id))->getField('template_content');
            //广告的具体内容
            $ad_contents = $this->db->where(array('status' => 1, 'ad_id' => $ad_id))->select();


            if(empty($ad_contents)){
                $info=array(
                    "status"=>0,
                    "error"=>"请先增加广告内容",
                );
            }


            if(empty($template_content)){
                $info=array(
                    "status"=>0,
                    "error"=>"请先给广告匹配模板"
                );
            }
            //只有当广告模板和广告内容都存在时，才生成JS文件
            if(!empty($template_content) && !empty($ad_contents)){
                //将广告模板中的替换标识 替换成 广告的具体内容
                $thumb = implode(',', array_column($ad_contents, 'thumb'));
                $url = implode(',', array_column($ad_contents, 'url'));
                $title = implode(',', array_column($ad_contents, 'title'));
                $description = implode(',', array_column($ad_contents, 'description'));

                $search_arr = array("{thumb}", "{url}", "{title}", "{description}");
                $replace_arr = array("'" . $thumb . "'", "'" . $url . "'", "'" . $title . "'", "'" . $description . "'");
                $js_content = str_replace($search_arr, $replace_arr, $template_content);
                $ad_name = I('ad_name');
                $path = "./Adcontent/" . $ad_name . ".js";
                file_put_contents($path, $js_content);
                $info=array(
                    "status"=>1,
                    "success"=>__ROOT__."/Adcontent/" . $ad_name . ".js",
                );
            }
            $this->ajaxReturn($info);
        }
    }
}